package com.zx.template.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zx.template.base.PageBean;
import com.zx.template.base.R;
import com.zx.template.base.ValidationGroup;
import com.zx.template.entity.VipConfigDO;
import com.zx.template.mapper.VipConfigMapper;
import com.zx.template.pojo.dto.VipConfigDto;
import com.zx.template.pojo.vo.VipConfigVO;
import com.zx.template.service.VipConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/** web-会员管理
 * @author szh
 * @date 2022/8/3 14:02
 */
@RestController
@RequestMapping("/vip")
@Api(tags = "web-会员管理")
public class MemberManagerController {


    @Resource
    VipConfigService vipConfigService;
    
    @Resource
    VipConfigMapper vipConfigMapper;

    @ApiOperation("获取 套餐 分页列表 ")
    @GetMapping("/page")
    public R<PageBean<VipConfigVO>> vipConfigPage(@Validated(ValidationGroup.Page.class) VipConfigDto dto) {
        return R.ok(vipConfigService.vipConfigPage(dto));
    }


    @ApiOperation("新增 套餐")
    @PostMapping
    public R<Boolean> addCombo(@Validated(ValidationGroup.Add.class) @RequestBody VipConfigDto dto)  {
        Assert.isFalse(NumberUtil.isGreaterOrEqual(dto.getOriginalPrice(),dto.getDiscountAmount()),"折扣金额不能大于原价金额");
        return vipConfigService.addCombo(dto);
    }
    @ApiOperation("更新 套餐")
    @PutMapping
    public R<Boolean> updateCombo(@Validated(ValidationGroup.Modify.class)  @RequestBody VipConfigDto dto) {
        return vipConfigService.updateCombo(dto);
    }


    @ApiOperation("查看 套餐 ")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataTypeClass =String.class,example = "123")
    @GetMapping("/{id}")
    public R<VipConfigVO> getVipCombo(@PathVariable String id) {
        return vipConfigService.getVipCombo(Convert.toLong(id));

    }

    @ApiOperation("删除 套餐 ")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataTypeClass =String.class,example = "123")
    @DeleteMapping("/{id}")
    public R<Boolean> delVipCombo(@PathVariable String  id) {
        return vipConfigService.delVipCombo(Convert.toLong(id));

    }

    @GetMapping("/test/{id}")
    public String test(@PathVariable Long id){
        // 在 model 设置 @TableField(updateStrategy = FieldStrategy.IGNORED) 前提下
        // 1-vipConfigMapper.update 传null sql为：UPDATE t_vip_config SET type_name=? WHERE deleted=0 AND (id = ?)
        // int ddd = vipConfigMapper.update(null, Wrappers.<VipConfigDO>lambdaUpdate().set(VipConfigDO::getTypeName, "ddd").eq(VipConfigDO::getId, id));

        // 2- vipConfigMapper.update(new VipConfigDO(),
        // sql 为 ：UPDATE t_vip_config SET type_name=?, original_price=?, discount_amount=?, real_price=?, days=?, top_days=?, state=?, create_time=?, update_time=?, type_name=? WHERE deleted=0 AND (id = ?)
        // Parameters: null, null, null, null, null, null, null, null, null, ddd(String), 1(Long)
        int ddd = vipConfigMapper.update(new VipConfigDO(), Wrappers.<VipConfigDO>lambdaUpdate().set(VipConfigDO::getTypeName, "ddd").eq(VipConfigDO::getId, id));
        return ddd+"";
    }



}
